package appfuse.framework.util;

/**
 * <p>Title: 通用的翻页控制器</p>
 * <p>Description: 资料库系统</p>
 * <p>Copyright: Kingsoft Copyright (c) 2004</p>
 * <p>Company: 金山软件</p>
 * @author savage
 * @version 1.0
 */
public class PageController {
    int totalRowsAmount; //总行数
    boolean rowsAmountSet; //是否设置过totalRowsAmount
    int pageSize = 2; //每页行数
    int currentPage = 1; //当前页码
    int nextPage; //下一页
    int previousPage; //上一页
    int totalPages; //总页数
    boolean hasNext; //是否有下一页
    boolean hasPrevious; //是否有前一页
    String description; //描述
    int pageStartRow; //页面开始行
    int pageEndRow; //页面结束行

    public PageController(int totalRows) {
        setTotalRowsAmount(totalRows);
    }

    public PageController() {}

    public void setTotalRowsAmount(int i) {
        //if (!this.rowsAmountSet) {
        totalRowsAmount = i;
        //totalPages = totalRowsAmount / pageSize + 1;
        //setCurrentPage(1);
        //this.rowsAmountSet = true;
        //}

    }

    public void setPageController() {
        totalPages = (totalRowsAmount + pageSize - 1) / pageSize;
        //当前页校正
        if(totalPages == 0)
            setCurrentPage(1);
        else if(currentPage <= 0)
            setCurrentPage(1);
        else if(currentPage > totalPages)
            setCurrentPage(totalPages);
        nextPage = currentPage + 1;
        previousPage = currentPage - 1;
        //计算当前页开始行和结束行
        if (currentPage * pageSize < totalRowsAmount) {
            pageEndRow = currentPage * pageSize;
            pageStartRow = pageEndRow - pageSize + 1;

        } else {
            pageEndRow = totalRowsAmount;
            pageStartRow = pageSize * (totalPages - 1) + 1;
            if(pageStartRow < 1) pageStartRow = 1;
        }
        if (nextPage > totalPages) {
            hasNext = false;
        } else {
            hasNext = true;
        }
        if (previousPage == 0) {
            hasPrevious = false;
        } else {
            hasPrevious = true;
        }
    }

    public void setCurrentPage(int i) {
        currentPage = i;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public boolean isHasPrevious() {
        return hasPrevious;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPreviousPage() {
        return previousPage;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public int getTotalRowsAmount() {
        return totalRowsAmount;
    }

    public void setHasNext(boolean b) {
        hasNext = b;
    }

    public void setHasPrevious(boolean b) {
        hasPrevious = b;
    }

    public void setNextPage(int i) {
        nextPage = i;
    }

    public void setPageSize(int i) {
        pageSize = i;
    }

    public void setPreviousPage(int i) {
        previousPage = i;
    }

    public void setTotalPages(int i) {
        totalPages = i;
    }

    public int getPageEndRow() {
        return pageEndRow;
    }

    public int getPageStartRow() {
        return pageStartRow;
    }

    public String getDescription() {
        StringBuffer description = new StringBuffer();
        description.append("共有 ").append(this.getTotalRowsAmount()).append(
            " 条记录")
            .append(" 共 ").append(this.getTotalPages()).append(" 页，")
            .append("当前是第 ").append(this.currentPage).append(" 页");
        return description.toString();
    }

    public String description() {
        String description = "Total:" + this.getTotalRowsAmount() +
            " items " + this.getTotalPages() + " pages,Current page:" +
            this.currentPage + " Previous " + this.hasPrevious +
            " Next:" + this.hasNext +
            " start row:" + this.pageStartRow +
            " end row:" + this.pageEndRow;
        return description;
    }

    public static void main(String args[]) {
        PageController pc = new PageController(3);
     
    }
}